﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.IO;
using System.Diagnostics;


namespace TMS
{
    public partial class LogonScreen : Form
    {
        public Users ActiveUser = new Users();

        public LogonScreen()
        {
            InitializeComponent();
            KorisnikText.Select();
                               
        }

        private void button1_Click(object sender, EventArgs e)
        {
            LogonScreen.ActiveForm.Close();
        } 


        private void LoginButton_Click(object sender, EventArgs e)
        {

            //ConfigLoad config = new ConfigLoad();

            //string cs = @"server=" + config.GetServer() + ";userid=" + config.GetUserID() + ";password=" + config.GetPassword() + ";database=" + config.GetDatabase();
            //string cs = @"server=localhost;userid=root;password=pass;database=mydb";
            string connString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;

            MySqlConnection conn = null;
            MySqlDataReader rdr = null;
            conn = new MySqlConnection(connString);
            conn.Open();

            string stm = @"SELECT Password, ID FROM Users WHERE Username='"+KorisnikText.Text+"'";
            MySqlCommand cmd = new MySqlCommand(stm, conn);
            rdr = cmd.ExecuteReader();

            if ((rdr.Read())&&(LozinkaText.Text == rdr.GetString(0)))
            {
                
                //int userRole = Convert.ToInt32(rdr.GetString(3));
                //ActiveUser.SetLevel(userRole);
                //ActiveUser.SetName(rdr.GetString(1));
                //ActiveUser.SetSurname(rdr.GetString(2));
                //ActiveUser.SetName(rdr.GetString(1));
                //ActiveUser.SetUsername(KorisnikText.Text);
                ActiveUser.SetID(rdr.GetString(1));
                ActiveUser.GetUserFromDB();
                //ActiveUser.SetPassword(LozinkaText.Text);
                Program.SetActiveUser(ActiveUser);
                KorisnikText.Text = "";
                LozinkaText.Text = "";
                KorisnikText.Select();

                    MainForm activeForm = new MainForm();
                    Program.SetMainForm(activeForm);
                    activeForm.ShowDialog();

                
                
            }
            else
            {
                MessageBox.Show("Не постои таков корисник");
            }
                 
            if (conn != null) 
            {
              conn.Close();
            }
        }

        private void LozinkaText_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                LoginButton_Click((object)sender, (EventArgs)e);
            }
            
        }





        
    }
}
